package com.google.code.ptrends.catparsers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.google.code.ptrends.catparsers.utils.RegexHelper;
import com.google.code.ptrends.common.entities.Category;
import com.google.code.ptrends.common.entities.Property;
import com.google.code.ptrends.common.entities.Session;
import com.google.code.ptrends.entities.ETLItem;
import com.google.code.ptrends.entities.ParametersStrings;
import com.google.code.ptrends.excel.ExcelReader;
import com.google.code.ptrends.excel.entities.Worksheet;

public class MonitorCategoryParser extends AbstractCategoryParser {

	private List<Matcher> matcher;

	public MonitorCategoryParser(ExcelReader reader, Worksheet worksheet,
			final Session session) {
		super(reader, worksheet, session);
		matcher = new ArrayList<Matcher>();
		matcher.add(Pattern.compile(RegexHelper.getRegex("mon1")).matcher(""));
		matcher.add(Pattern.compile(RegexHelper.getRegex("mon2")).matcher(""));
	}

	@Override
	protected ETLItem createItem(final String description, final String priceUSD) {
		ETLItem item = null;
		if (StringUtils.isNotBlank(description)
				&& StringUtils.isNotBlank(priceUSD)) {

			for (Matcher m : matcher) {
				m.reset(description);
				if (m.find()) {

					item = buildItem(Category.MONITOR, m.group(2), m.group(3),
							new Float(priceUSD), new Date());
					final java.util.List<Property> property = new ArrayList<Property>();

					// 11--1 Wide screen size
					if (StringUtils.isNotBlank(m.group(1))) {
						property.add(buildProperty(ParametersStrings.DIAGONAL,
								m.group(1)));
					}

					// 12--4 Display Resolution
					if (StringUtils.isNotBlank(m.group(4))) {
						property.add(buildProperty(
								ParametersStrings.RESOLUTION, m.group(4)));
					}

					// 15--5 Brightness
					if (StringUtils.isNotBlank(m.group(5))) {
						property.add(buildProperty(
								ParametersStrings.BRIGHTNESS, m.group(5)));
					}

					// 13--7 Response time
					if (StringUtils.isNotBlank(m.group(6))) {
						property.add(buildProperty(
								ParametersStrings.RESPONSE_TIME, m.group(6)));
					}

					// 14--8 Viewing angle
					if (StringUtils.isNotBlank(m.group(7))) {
						property.add(buildProperty(ParametersStrings.ANGLE,
								m.group(7)));
					}

					item.setParameters(property);

					return item;
				}
			}
		}
		return item;
	}

}
